package com.ff.javamonkey.xmodem;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import Serialio.zmodem.ZModemListener;
import Serialio.zmodem.ZModemPort;
import android.util.Log;

public class ZModem implements ZModemListener {
	// Debugging
	private static final String TAG = "BluetoothTestActivity";
	
	private long curSize;
	private long begTime = 0;
	
	// TLV protocol specific
	private byte RTS = 0x02;
	private byte CTS = 0x03;
	
	public ZModem() {
		
	}
	
	public ZModem(String sndName, OutputStream sSnd, InputStream sRcv) {
		ZModemPort zm1 = new ZModemPort(sSnd, sRcv);
		zm1.addZModemListener(this);
		zm1.openLogFile("/sdcard/zmlog.txt");
		
		Log.i(TAG, "full path:" + sndName);
		
		try {
			File snd = new File(sndName);
			zm1.send(snd);
		}
		catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}
	
	public void transferBegin(String fname, long fileSize) {
//		System.out.println("Sending "+fname+", size: "+fileSize);
		Log.i(TAG, "Sending " +fname + ", size: " + fileSize);
		curSize = fileSize;
		begTime = System.currentTimeMillis();
	}

	public void transferData(long currentBytes) {
//		System.out.print("Bytes sent: "+ currentBytes +"\r");
		Log.i(TAG, "Bytes sent: " + currentBytes + "\r");
	}

	public void transferDone() {
		long endTime = System.currentTimeMillis();
		long totTime = endTime - begTime;
//		System.out.println("\nSend Time: "+totTime);
		Log.i(TAG,"SendTime: " + totTime);
//		Runtime.getRuntime().exit (-1);
	}	
}
